package com.zhangyu.shadow.repair.delegate.search;

import com.alibaba.fastjson.TypeReference;
import com.zhangyu.shadow.repair.base.IStringDataConvert;
import com.zhangyu.shadow.repair.config.RequestUrls;
import com.zhangyu.shadow.repair.delegate.bean.BusBean;
import com.zhangyu.shadow.repair.delegate.search.entity.MaintainEntity;
import com.zhangyu.shadow.repair.intercept.DataIntercept;
import com.zhangyu.shadow.repair.manager.AccountManager;

import java.util.ArrayList;

import io.reactivex.disposables.Disposable;
import st.lib.base.net.callback.IError;
import st.lib.base.net.callback.ISuccess;
import st.lib.base.net.rx.RxRestClient;
import st.lib.base.ui.recycler.MultipleItemEntity;
import st.lib.base.ui.recycler.ObjConverter;

/**
 * Description:
 *
 * @author Hequn.Lee
 * @date 2018/11/7
 */
public class SearchBusModel implements SearchBusContract.Model {
    @Override
    public Disposable searchBus(String code, final IStringDataConvert<ArrayList<MultipleItemEntity>> convert, IError error) {
        return RxRestClient.builder()
                .url(RequestUrls.URL_MAINTAIN_SEARCH_VEHICLE)
                .params("vehicleCode", code)
                .params(AccountManager.getInstance().getBaseInfo())
                .interceptor(new DataIntercept())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        if (response != null) {
                            final MaintainDataConverter converter = new MaintainDataConverter();
                            converter.setJsonData(response);
                            convert.convert(converter.convert());
                        } else {
                            convert.convert(new ArrayList<MultipleItemEntity>());
                        }
                    }
                })
                .error(error)
                .build()
                .post();
    }

    @Override
    public Disposable searchCode(String code, final IStringDataConvert<ArrayList<MultipleItemEntity>> convert, IError error) {
        return RxRestClient.builder()
                .url(RequestUrls.URL_MAINTAIN_SEARCH_VEHICLE)
                .params("vehicleCode", code)
                .params(AccountManager.getInstance().getBaseInfo())
                .interceptor(new DataIntercept())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        if (response != null) {
                            final MaintainDataConverter converter = new MaintainDataConverter();
                            converter.setJsonData(response);
                            convert.convert(converter.convert());
                        } else {
                            convert.convert(new ArrayList<MultipleItemEntity>());
                        }
                    }
                })
                .error(error)
                .build()
                .post();
    }

    @Override
    public Disposable getBusList(String depId,
//                                 String groupId,
                                 String dateStart,
                                 String dateEnd,
                                 final IStringDataConvert<ArrayList<BusBean>> convert,
                                 IError error) {
        return RxRestClient.builder()
                .url(RequestUrls.URL_MAINTAIN_BUS_LIST)
                .params(AccountManager.getInstance().getBaseInfo())
                .params("dateStart", dateStart)
                .params("dateEnd", dateEnd)
                .params("deptId", depId)
                .interceptor(new DataIntercept())
//                .params("groupId", groupId)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        convert.convert(convertBusListBean(response));
                    }
                })
                .error(error)
                .build()
                .post();
    }

    private ArrayList<BusBean> convertBusListBean(String response) {
        return new ObjConverter<ArrayList<BusBean>>().convert(response
                , new TypeReference<ArrayList<BusBean>>() {
                });
    }

    private MaintainEntity convertSearchMaintainBean(String response) {
        return new ObjConverter<MaintainEntity>().convert(response,
                new TypeReference<MaintainEntity>() {
                });
    }
}
